home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / sc.zoo / psc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-03  |  5.2 KB  |  298 lines

  1. /* Sc parse routine
  2.  *
  3.  * usage psc options
  4.  * options:
  5.  *   -L        Left justify strings.  Default is right justify.
  6.  *   -r        Assemble data into rows first, not columns.
  7.  *   -R    n    Increment by n between rows 
  8.  *   -C n    Increment by n between columns
  9.  *   -n n    Length of the row (column) should be n.
  10.  *   -s v    Top left location in the spreadsheet should be v; eg, k5
  11.  *   -d c       Use c as the delimiter between the fields.
  12.  *   -k         Keep all delimiters - Default is strip multiple delimiters to 1.
  13.  *   
  14.  *  Author: Robert Bond
  15.  */
  16.  
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include "sc.h"
  20.  
  21. #define END 0
  22. #define NUM 1
  23. #define ALPHA 2
  24. #define SPACE 3
  25. #define EOL 4
  26.  
  27. extern char *optarg;
  28. extern int   optind;
  29. char *coltoa();
  30. char *progname;
  31.  
  32. int colfirst = 0;
  33. int r0 = 0;
  34. int c0 = 0;
  35. int rinc = 1;
  36. int cinc = 1;
  37. int leftadj = 0;
  38. int len = 20000;
  39. char delim1 = ' ';
  40. char delim2 = '\t';
  41. int strip_delim = 1;
  42. int fwidth[MAXCOLS];
  43. int precision[MAXCOLS];
  44.  
  45. char token[1000];
  46.  
  47. main(argc, argv)
  48. int argc;
  49. char **argv;
  50. {
  51.     int curlen;
  52.     int curcol, coff;
  53.     int currow, roff;
  54.     int first;
  55.     int c;
  56.     register effr, effc;
  57.     int i,j;
  58.     register char *p;
  59.  
  60.     progname = argv[0];
  61.     while ((c = getopt(argc, argv, "rLks:R:C:n:d:")) != EOF) {
  62.     switch(c) {
  63.     case 'r':
  64.         colfirst = 1;
  65.         break;
  66.     case 'L':
  67.         leftadj = 1;
  68.         break;
  69.     case 's':
  70.         c0 = getcol(optarg);
  71.         r0 = getrow(optarg);
  72.         break;
  73.     case 'R':
  74.         rinc = atoi(optarg);
  75.         break;
  76.     case 'C':
  77.         cinc = atoi(optarg);
  78.         break;
  79.     case 'n':
  80.         len = atoi(optarg);
  81.         break;
  82.     case 'd':
  83.         delim1 = optarg[0];
  84.         delim2 = 0;
  85.         break;
  86.     case 'k':
  87.         strip_delim = 0;
  88.         break;
  89.     default:
  90.         (void) fprintf(stderr,"Usage: %s [-rkL] [-s v] [-R i] [-C i] [-n i] [-d c]\n", progname);
  91.         exit(1);
  92.         }
  93.     }
  94.  
  95.     if (optind < argc) {
  96.         (void) fprintf(stderr,"Usage: %s [-rL] [-s v] [-R i] [-C i] [-n i] [-d c]\n", progname);
  97.         exit(1);
  98.     }
  99.  
  100.     curlen = 0;
  101.     curcol = c0; coff = 0;
  102.     currow = r0; roff = 0;
  103.     first = 1;
  104.  
  105.     while(1) {
  106.  
  107.     effr = currow+roff;
  108.     effc = curcol+coff;
  109.  
  110.     switch(scan()) {
  111.     case END:
  112.         for (i = 0; i<MAXCOLS; i++) {
  113.         if (precision[i])
  114.             (void) printf("format %s %d %d\n", coltoa(i), precision[i]+1,
  115.             fwidth[i]);
  116.         }
  117.         exit(0);
  118.     case NUM:
  119.         first = 0;
  120.         (void) printf("let %s%d = %s\n", coltoa(effc), effr, token);
  121.         if (effc > MAXCOLS-1)
  122.         (void) fprintf(stderr, "Invalid column used: %s\n", coltoa(effc));
  123.         else {
  124.         i = 0;
  125.         j = 0;
  126.         p = token;
  127.         while (*p && *p != '.') {
  128.             p++; i++;
  129.         }
  130.         if (*p) {
  131.             p++; i++;
  132.         }
  133.         while (*p) {
  134.             p++; i++; j++;
  135.         }
  136.         if (precision[effc] < i)
  137.             precision[effc] = i;
  138.         if (fwidth[effc] < j)
  139.             fwidth[effc] = j;
  140.         }
  141.         break;
  142.     case ALPHA:
  143.         first = 0;
  144.         if (leftadj)
  145.         (void) printf("leftstring %s%d = \"%s\"\n", coltoa(effc),effr,token); 
  146.         else
  147.         (void) printf("rightstring %s%d = \"%s\"\n",coltoa(effc),effr,token); 
  148.         if (effc > MAXCOLS-1)
  149.         (void) fprintf(stderr, "Invalid column used: %s\n", coltoa(effc));
  150.         else {
  151.         i = strlen(token);
  152.         if (i > precision[effc])
  153.             precision[effc] = i;
  154.         }
  155.         break;
  156.     case SPACE:
  157.         if (first && strip_delim)
  158.         break;
  159.         if (colfirst)
  160.         roff++;
  161.         else
  162.         coff++;
  163.         break;
  164.     case EOL:
  165.         curlen++;
  166.         roff = 0;
  167.         coff = 0;
  168.         first = 1;
  169.         if (colfirst) {
  170.         if (curlen >= len) {
  171.             curcol = c0;
  172.             currow += rinc;
  173.             curlen = 0;
  174.         } else {
  175.             curcol += cinc;
  176.         }
  177.         } else {
  178.         if (curlen >= len) {
  179.             currow = r0;
  180.             curcol += cinc;
  181.             curlen = 0;
  182.         } else {
  183.             currow += rinc;
  184.         }
  185.         }
  186.         break;
  187.     }
  188.     }
  189. }
  190.  
  191. scan()
  192. {
  193.     register int c;
  194.     register char *p;
  195.  
  196.     p = token;
  197.     c = getchar();
  198.  
  199.     if (c == EOF)
  200.     return(END);
  201.  
  202.     if (c == '\n')
  203.     return(EOL);
  204.  
  205.     if (c == delim1 || c == delim2) {
  206.         if (strip_delim) {
  207.         while ((c = getchar()) && (c == delim1 || c == delim2))
  208.             ;
  209.         (void)ungetc(c, stdin);
  210.     } 
  211.     return(SPACE);
  212.     }
  213.  
  214.     if (c == '\"') {
  215.     while ((c = getchar()) && c != '\"' && c != '\n' && c != EOF)
  216.         *p++ = c;
  217.     if (c != '\"')
  218.         (void)ungetc(c, stdin);
  219.     *p = 0;
  220.     return(ALPHA);
  221.     }
  222.  
  223.     while (c != delim1 && c != delim2 && c!= '\n' && c != EOF) {
  224.     *p++ = c;
  225.     c = getchar();
  226.     }
  227.     *p = 0;
  228.     (void)ungetc(c, stdin);
  229.  
  230.     p = token;
  231.     c = *p;
  232.     if (isdigit(c) || c == '.' || c == '-' || c == '+') {
  233.     while(isdigit(c) || c == '.' || c == '-' || c == '+' || c == 'e'
  234.         || c == 'E') {
  235.         c = *p++;
  236.     }
  237.     if (c == 0)
  238.         return(NUM);
  239.     else
  240.         return(ALPHA);
  241.     }
  242.  
  243.     return(ALPHA);
  244. }
  245.     
  246. getcol(p)
  247. char *p;
  248. {
  249.     register  col;
  250.  
  251.     if (!p)
  252.     return(0);
  253.     while(*p && !isalpha(*p)) 
  254.     p++; 
  255.     if (!*p)
  256.     return(0);
  257.     col = ((*p & 0137) - 'A');
  258.     if (isalpha(*++p)) 
  259.     col = (col + 1)*26 + ((*p & 0137) - 'A');
  260.     return(col);
  261. }
  262.  
  263. getrow(p)
  264. char *p;
  265. {
  266.     int row;
  267.  
  268.     if (!p)
  269.     return(0);
  270.     while(*p && !isdigit(*p))
  271.     p++; 
  272.     if (!*p)
  273.     return(0);
  274.     if (sscanf(p, "%d", &row) != 1)
  275.     return(0);
  276.     return(row);
  277. }
  278.  
  279. char *
  280. coltoa(col)
  281. int col;
  282. {
  283.     static char rname[3];
  284.     register char *p = rname;
  285.  
  286.     if (col < 0 || col > 25*26) 
  287.     (void) fprintf(stderr,"coltoa: invalid col: %d", col);
  288.  
  289.     if (col > 25) {
  290.     *p++ = col/26 + 'A' - 1;
  291.     col %= 26;
  292.     }
  293.     *p++ = col+'A';
  294.     *p = 0;
  295.     return(rname);
  296. }
  297.  
  298.